﻿using System;
using System.Collections.Generic;
using System.Data;
using BLL;
using Utils;

namespace UI.ModSalesman
{
    public partial class RewardsAdd : System.Web.UI.Page
    {
        SalesmanBiz salesmanBiz = new SalesmanBiz();
        DefDicBiz defDicBiz = new DefDicBiz();
        string rewardsID = "0";
        string account = "";
        string saleManID = "0"; //通过人员列表界面，弹出界面来获得基本信息
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] != null)
            {
                User user = Session["user"] as User;
                account = user.Account;
            }
            else
            {
                JSUtility.Alert(this, "您没有登录!请先登录。");
                return;
            }
            if (Request.Params["SaleManID"] != null)
            {
                saleManID = Request.Params["SaleManID"].ToString();
            }
            if (Request.Params["RewardsID"] != null)
            {
                rewardsID = Request.Params["RewardsID"].ToString();
                btnAdd.Enabled = false;
            }
            else
            {
                btnUpdate.Enabled = false;
                btnDelete.Enabled = false;
            }
            if (!IsPostBack)
            {
                JSUtility.AddConfirm(btnDelete, "您真的要删除该信息吗？");
                txtRewardsTime.Text = System.DateTime.Now.ToString("yyyy-MM-dd");
                if (rewardsID != "0") BindData(rewardsID);
                else if (saleManID != "0")
                {
                    DataTable dt = salesmanBiz.GetOneInfo(saleManID); ;
                    FillData(dt);
                }

            }
        }
        private void BindData(string rewardsID)
        {
            DataTable dt = salesmanBiz.GetRewardsOneInfo(rewardsID);
            if (dt.Rows.Count > 0)
            {
                hidSaleManID.Value = dt.Rows[0]["SaleManID"].ToString();
                txtEmpCode.Text = dt.Rows[0]["EmpCode"].ToString();
                txtDeptName.Text = dt.Rows[0]["DeptName"].ToString();
                txtDutyName.Text = dt.Rows[0]["DutyName"].ToString();
                txtEmpName.Text = dt.Rows[0]["EmpName"].ToString();
                txtStateName.Text = dt.Rows[0]["StateName"].ToString();
                defDicBiz.InitSynDropDownList(ddlType, dt.Rows[0]["RewardsType"].ToString().Trim());

                txtRewardsAmount.Text = dt.Rows[0]["RewardsAmount"].ToString();
                txtRewardsReason.Text = dt.Rows[0]["RewardsReason"].ToString();
                txtRewardsTime.Text = dt.Rows[0]["RewardsTime"].ToString();
  
                dt.Dispose();
            }
            else
            {
                JSUtility.Alert(this, "无数据");
            }

        }
        protected void txtEmpCode_TextChanged(object sender, EventArgs e)
        {
            string empCode = txtEmpCode.Text.Trim();
            if (!string.IsNullOrWhiteSpace(empCode))
            {
                if (Utils.StringUtility.IncludeHZ(empCode))//如果包含汉字
                {
                    DataTable dt = salesmanBiz.GetOneInfoByName(empCode,"在职");
                    if (dt.Rows.Count == 0)
                    {
                        JSUtility.Alert(this, "不存在该员工姓名: " + empCode);
                    }
                    else if (dt.Rows.Count == 1)
                    {
                        hidSaleManID.Value = dt.Rows[0]["SaleManID"].ToString();
                        txtEmpCode.Text = dt.Rows[0]["EmpCode"].ToString();
                        txtDeptName.Text = dt.Rows[0]["DeptName"].ToString();
                        txtDutyName.Text = dt.Rows[0]["DutyName"].ToString();
                        txtEmpName.Text = dt.Rows[0]["EmpName"].ToString();
                        txtStateName.Text = dt.Rows[0]["StateName"].ToString();
                    }
                    else
                    {
                        string info = "存在同名，请输入工号：\n\n";
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            info += dt.Rows[i]["EmpCode"].ToString() + "  " + dt.Rows[i]["EmpName"].ToString() + "\n";
                        }
                        JSUtility.Alert(this, info);
                    }
                }
                else
                {
                    DataTable dt = salesmanBiz.GetOneInfo(empCode,"在职");
                    if (dt.Rows.Count > 0)
                    {
                        hidSaleManID.Value = dt.Rows[0]["SaleManID"].ToString();
                        txtDeptName.Text = dt.Rows[0]["DeptName"].ToString();
                        txtDutyName.Text = dt.Rows[0]["DutyName"].ToString();
                        txtEmpName.Text = dt.Rows[0]["EmpName"].ToString();
                        txtStateName.Text = dt.Rows[0]["StateName"].ToString();
                    }
                }
            }
        }
        protected void FillData(DataTable dt)
        {
            if (dt.Rows.Count == 1)
            {
                hidSaleManID.Value = dt.Rows[0]["SaleManID"].ToString();
                txtEmpCode.Text = dt.Rows[0]["EmpCode"].ToString();
                txtDeptName.Text = dt.Rows[0]["DeptName"].ToString();
                txtDutyName.Text = dt.Rows[0]["DutyName"].ToString();
                txtEmpName.Text = dt.Rows[0]["EmpName"].ToString();
                txtStateName.Text = dt.Rows[0]["StateName"].ToString();
            }
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string empCode = txtEmpCode.Text.Trim();
            if (string.IsNullOrWhiteSpace(empCode))
            {
                JSUtility.Alert(this, "售货员编号不能为空");
                return;
            }
            if (!salesmanBiz.IsExistEmpCode(empCode, "在职"))
            {
                JSUtility.Alert(this, "不存在该售货员编号");
                return;
            }
            if (!RegexRule.IsDecimal(txtRewardsAmount.Text.Trim()))
            {
                JSUtility.Alert(this, "金额必须为数字");
                return;
            }
            string hidID = hidSaleManID.Value;
            if (string.IsNullOrEmpty(hidID))
            {
                DataTable idtb = salesmanBiz.GetOneInfo(empCode, "在职");
                if (idtb.Rows.Count > 0)
                {
                    hidID = idtb.Rows[0]["SaleManID"].ToString();
                }
                else
                {
                    JSUtility.Alert(this, "输入的工号不存在！");
                    return;
                }
            }
            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
            sdList.Add("SaleManID", hidID);
            sdList.Add("EmpCode", empCode);
            sdList.Add("EmpName", txtEmpName.Text.Trim());
            sdList.Add("DeptName", txtDeptName.Text.Trim());
            sdList.Add("DutyName", txtDutyName.Text.Trim());
            sdList.Add("StateName", txtStateName.Text.Trim());
            sdList.Add("RewardsType", ddlType.SelectedValue);
            sdList.Add("RewardsTime", txtRewardsTime.Text.Trim());
            sdList.Add("RewardsAmount", (txtRewardsAmount.Text.Trim() == "") ? "0" : txtRewardsAmount.Text.Trim());
            sdList.Add("RewardsReason", txtRewardsReason.Text.Trim());           
            sdList.Add("Account", account);

            if (salesmanBiz.Insert("Admin_Salesman_Rewards", sdList))
                JSUtility.AlertAndRedirect("增加信息成功", "RewardsAdd.aspx");//Request.Url.PathAndQuery
            else
                JSUtility.Alert(this, "增加信息失败");
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (rewardsID == "0")
            {
                JSUtility.Alert(this, "没有选择记录无法更改");
                return;
            }
            string empCode = txtEmpCode.Text.Trim();
            if (string.IsNullOrWhiteSpace(empCode))
            {
                JSUtility.Alert(this, "售货员编号不能为空");
                return;
            }
            if (!salesmanBiz.IsExistEmpCode(empCode, "在职"))
            {
                JSUtility.Alert(this, "不存在该售货员编号");
                return;
            }
            if (!RegexRule.IsDecimal(txtRewardsAmount.Text.Trim()))
            {
                JSUtility.Alert(this, "金额必须为数字");
                return;
            }
            if (!RegexRule.IsDecimal(txtRewardsAmount.Text.Trim()))
            {
                JSUtility.Alert(this, "金额必须为数字");
                return;
            } 
            if (string.IsNullOrEmpty(hidSaleManID.Value))
            {
                JSUtility.Alert(this, "请从列表重新进入该界面，进行更新操作！");
                return;
            }
            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();

            sdList.Add("RewardsID", rewardsID);
            sdList.Add("SaleManID", hidSaleManID.Value);
            sdList.Add("EmpCode", empCode);
            sdList.Add("EmpName", txtEmpName.Text.Trim());
            sdList.Add("DeptName", txtDeptName.Text.Trim());
            sdList.Add("DutyName", txtDutyName.Text.Trim());
            sdList.Add("StateName", txtStateName.Text.Trim());
            sdList.Add("RewardsType", ddlType.SelectedValue);
            sdList.Add("RewardsTime", txtRewardsTime.Text.Trim());
            sdList.Add("RewardsAmount", (txtRewardsAmount.Text.Trim() == "") ? "0" : txtRewardsAmount.Text.Trim());
            sdList.Add("RewardsReason", txtRewardsReason.Text.Trim());
         
            sdList.Add("Account", account);
            if (rewardsID != "0")
            {
                string info = "";
                if (salesmanBiz.Update("Admin_Salesman_Rewards", sdList))
                {
                    info = "成功更新信息";
                    BindData(rewardsID);
                }
                else
                    info = "更新信息失败";
                JSUtility.Alert(this, info);
            }
            else
                JSUtility.Alert(this, "请先选择要修改的信息");
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (rewardsID != "0")
            {
                string info = "";
                if (salesmanBiz.DeleteRewardsOneInfo(rewardsID))
                    info = "删除成功";
                else
                    info = "删除失败";
                JSUtility.AlertAndRedirect(info, "SalesmanList.aspx");
            }
            else
                JSUtility.Alert(this, "没有选择记录无法删除");
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            JSUtility.CloseWindow();
        }

    }
}